package com.zjdiepu.www.biz.partner;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.partner.ThRepaymentScheduleMapper;
import com.zjdiepu.www.ibiz.partner.IThRepaymentScheduleService;
import com.zjdiepu.www.model.partner.dto.ThRepaymentSchedule;
import com.zjdiepu.www.model.partner.ext.ThRepaymentScheduleExt;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * 第三方募集信息Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年09月28日 10时13分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThRepaymentScheduleServiceImpl extends BaseService implements IThRepaymentScheduleService {
	public static final Logger logger = LoggerFactory.getLogger(ThRepaymentScheduleServiceImpl.class);

	@Autowired
	private ThRepaymentScheduleMapper thRepaymentScheduleMapper;
	
	@Override
	public void saveUpdateAndDelete(ThRepaymentSchedule thRepaymentSchedule, OperateType operType) {
		switch (operType) {
		case add:
			thRepaymentSchedule.initInsertData(thRepaymentSchedule.getModifier(), thRepaymentSchedule.getModifierName());
			thRepaymentScheduleMapper.insert(thRepaymentSchedule);
			break;
		case edit:
			thRepaymentSchedule.initUpdateData(thRepaymentSchedule.getModifier(), thRepaymentSchedule.getModifierName());
			// 影响的记录数
			int updateCount = thRepaymentScheduleMapper.updateByPrimaryKeySelective(thRepaymentSchedule);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thRepaymentSchedule.initDeleteData(thRepaymentSchedule.getModifier(), thRepaymentSchedule.getModifierName());
			// 影响的记录数
			int deleteCount = thRepaymentScheduleMapper.updateByPrimaryKeySelective(thRepaymentSchedule);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThRepaymentSchedule> selectByExt(ThRepaymentScheduleExt thRepaymentScheduleExt) {
		return thRepaymentScheduleMapper.selectByExt(thRepaymentScheduleExt);
	}
	
	@Override
	public int countByExt(ThRepaymentScheduleExt thRepaymentScheduleExt){
		return thRepaymentScheduleMapper.countByExt(thRepaymentScheduleExt);
	}
	
	@Override
	public ThRepaymentSchedule selectById(String id) {
		return thRepaymentScheduleMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thRepaymentScheduleMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThRepaymentSchedule thRepaymentSchedule) {
		return thRepaymentScheduleMapper.countRefined(thRepaymentSchedule);
	}

	@Override
	public ThRepaymentSchedule selectRefined(ThRepaymentSchedule thRepaymentSchedule) {
		return thRepaymentScheduleMapper.selectRefined(thRepaymentSchedule);
	}

	@Override
	public boolean repeat(ThRepaymentSchedule thRepaymentSchedule) {
		int count = countRefined(thRepaymentSchedule);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thRepaymentSchedule.getId())) {
			return true;
		}
		ThRepaymentSchedule dbItem = selectRefined(thRepaymentSchedule);
		if(null == dbItem) {
			return false;
		}
		return !thRepaymentSchedule.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thRepaymentScheduleMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return thRepaymentScheduleMapper.countMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<ThRepaymentSchedule> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return thRepaymentScheduleMapper.selectMultiByExt(ywBusinessQueryRequest);
	}
}
